<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>django.db.transaction &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../../index.html" />
    <link rel="up" title="django.db" href="../db.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-db-transaction">
            
  <h1>Source code for django.db.transaction</h1><div class="highlight"><pre>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">This module implements a transaction manager that can be used to define</span>
<span class="sd">transaction handling in a request or view function. It is used by transaction</span>
<span class="sd">control middleware and decorators.</span>

<span class="sd">The transaction manager can be in managed or in auto state. Auto state means the</span>
<span class="sd">system is using a commit-on-save strategy (actually it&#39;s more like</span>
<span class="sd">commit-on-change). As soon as the .save() or .delete() (or related) methods are</span>
<span class="sd">called, a commit is made.</span>

<span class="sd">Managed transactions don&#39;t do those commits, but will need some kind of manual</span>
<span class="sd">or implicit commits or rollbacks.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">warnings</span>

<span class="kn">from</span> <span class="nn">functools</span> <span class="kn">import</span> <span class="n">wraps</span>

<span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="p">(</span>
    <span class="n">connections</span><span class="p">,</span> <span class="n">DEFAULT_DB_ALIAS</span><span class="p">,</span>
    <span class="n">DatabaseError</span><span class="p">,</span> <span class="n">Error</span><span class="p">,</span> <span class="n">ProgrammingError</span><span class="p">)</span>
<span class="kn">from</span> <span class="nn">django.utils.decorators</span> <span class="kn">import</span> <span class="n">available_attrs</span>
<span class="kn">from</span> <span class="nn">django.utils.deprecation</span> <span class="kn">import</span> <span class="n">RemovedInDjango18Warning</span>


<div class="viewcode-block" id="TransactionManagementError"><a class="viewcode-back" href="../../../ref/exceptions.html#django.db.transaction.TransactionManagementError">[docs]</a><span class="k">class</span> <span class="nc">TransactionManagementError</span><span class="p">(</span><span class="n">ProgrammingError</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This exception is thrown when transaction management is used improperly.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">pass</span>


<span class="c">################</span>
<span class="c"># Private APIs #</span>
<span class="c">################</span>
</div>
<span class="k">def</span> <span class="nf">get_connection</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Get a database connection by name, or the default database connection</span>
<span class="sd">    if no name is provided.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">using</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">using</span> <span class="o">=</span> <span class="n">DEFAULT_DB_ALIAS</span>
    <span class="k">return</span> <span class="n">connections</span><span class="p">[</span><span class="n">using</span><span class="p">]</span>


<span class="c">###########################</span>
<span class="c"># Deprecated private APIs #</span>
<span class="c">###########################</span>

<span class="k">def</span> <span class="nf">abort</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Roll back any ongoing transactions and clean the transaction management</span>
<span class="sd">    state of the connection.</span>

<span class="sd">    This method is to be used only in cases where using balanced</span>
<span class="sd">    leave_transaction_management() calls isn&#39;t possible. For example after a</span>
<span class="sd">    request has finished, the transaction state isn&#39;t known, yet the connection</span>
<span class="sd">    must be cleaned up for the next request.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">get_connection</span><span class="p">(</span><span class="n">using</span><span class="p">)</span><span class="o">.</span><span class="n">abort</span><span class="p">()</span>


<span class="k">def</span> <span class="nf">enter_transaction_management</span><span class="p">(</span><span class="n">managed</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">forced</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Enters transaction management for a running thread. It must be balanced with</span>
<span class="sd">    the appropriate leave_transaction_management call, since the actual state is</span>
<span class="sd">    managed as a stack.</span>

<span class="sd">    The state and dirty flag are carried over from the surrounding block or</span>
<span class="sd">    from the settings, if there is no surrounding block (dirty is always false</span>
<span class="sd">    when no current block is running).</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">get_connection</span><span class="p">(</span><span class="n">using</span><span class="p">)</span><span class="o">.</span><span class="n">enter_transaction_management</span><span class="p">(</span><span class="n">managed</span><span class="p">,</span> <span class="n">forced</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">leave_transaction_management</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Leaves transaction management for a running thread. A dirty flag is carried</span>
<span class="sd">    over to the surrounding block, as a commit will commit all changes, even</span>
<span class="sd">    those from outside. (Commits are on connection level.)</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">get_connection</span><span class="p">(</span><span class="n">using</span><span class="p">)</span><span class="o">.</span><span class="n">leave_transaction_management</span><span class="p">()</span>


<span class="k">def</span> <span class="nf">is_dirty</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns True if the current transaction requires a commit for changes to</span>
<span class="sd">    happen.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">get_connection</span><span class="p">(</span><span class="n">using</span><span class="p">)</span><span class="o">.</span><span class="n">is_dirty</span><span class="p">()</span>


<span class="k">def</span> <span class="nf">set_dirty</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Sets a dirty flag for the current thread and code streak. This can be used</span>
<span class="sd">    to decide in a managed block of code to decide whether there are open</span>
<span class="sd">    changes waiting for commit.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">get_connection</span><span class="p">(</span><span class="n">using</span><span class="p">)</span><span class="o">.</span><span class="n">set_dirty</span><span class="p">()</span>


<span class="k">def</span> <span class="nf">set_clean</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Resets a dirty flag for the current thread and code streak. This can be used</span>
<span class="sd">    to decide in a managed block of code to decide whether a commit or rollback</span>
<span class="sd">    should happen.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">get_connection</span><span class="p">(</span><span class="n">using</span><span class="p">)</span><span class="o">.</span><span class="n">set_clean</span><span class="p">()</span>


<span class="k">def</span> <span class="nf">is_managed</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s">&quot;&#39;is_managed&#39; is deprecated.&quot;</span><span class="p">,</span>
        <span class="n">RemovedInDjango18Warning</span><span class="p">,</span> <span class="n">stacklevel</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">managed</span><span class="p">(</span><span class="n">flag</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s">&quot;&#39;managed&#39; no longer serves a purpose.&quot;</span><span class="p">,</span>
        <span class="n">RemovedInDjango18Warning</span><span class="p">,</span> <span class="n">stacklevel</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">commit_unless_managed</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s">&quot;&#39;commit_unless_managed&#39; is now a no-op.&quot;</span><span class="p">,</span>
        <span class="n">RemovedInDjango18Warning</span><span class="p">,</span> <span class="n">stacklevel</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">rollback_unless_managed</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s">&quot;&#39;rollback_unless_managed&#39; is now a no-op.&quot;</span><span class="p">,</span>
        <span class="n">RemovedInDjango18Warning</span><span class="p">,</span> <span class="n">stacklevel</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>


<span class="c">###############</span>
<span class="c"># Public APIs #</span>
<span class="c">###############</span>

<div class="viewcode-block" id="get_autocommit"><a class="viewcode-back" href="../../../topics/db/transactions.html#django.db.transaction.get_autocommit">[docs]</a><span class="k">def</span> <span class="nf">get_autocommit</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Get the autocommit status of the connection.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">get_connection</span><span class="p">(</span><span class="n">using</span><span class="p">)</span><span class="o">.</span><span class="n">get_autocommit</span><span class="p">()</span>

</div>
<div class="viewcode-block" id="set_autocommit"><a class="viewcode-back" href="../../../topics/db/transactions.html#django.db.transaction.set_autocommit">[docs]</a><span class="k">def</span> <span class="nf">set_autocommit</span><span class="p">(</span><span class="n">autocommit</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Set the autocommit status of the connection.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">get_connection</span><span class="p">(</span><span class="n">using</span><span class="p">)</span><span class="o">.</span><span class="n">set_autocommit</span><span class="p">(</span><span class="n">autocommit</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="commit"><a class="viewcode-back" href="../../../topics/db/transactions.html#django.db.transaction.commit">[docs]</a><span class="k">def</span> <span class="nf">commit</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Commits a transaction and resets the dirty flag.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">get_connection</span><span class="p">(</span><span class="n">using</span><span class="p">)</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span>

</div>
<div class="viewcode-block" id="rollback"><a class="viewcode-back" href="../../../topics/db/transactions.html#django.db.transaction.rollback">[docs]</a><span class="k">def</span> <span class="nf">rollback</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Rolls back a transaction and resets the dirty flag.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">get_connection</span><span class="p">(</span><span class="n">using</span><span class="p">)</span><span class="o">.</span><span class="n">rollback</span><span class="p">()</span>

</div>
<div class="viewcode-block" id="savepoint"><a class="viewcode-back" href="../../../topics/db/transactions.html#django.db.transaction.savepoint">[docs]</a><span class="k">def</span> <span class="nf">savepoint</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Creates a savepoint (if supported and required by the backend) inside the</span>
<span class="sd">    current transaction. Returns an identifier for the savepoint that will be</span>
<span class="sd">    used for the subsequent rollback or commit.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">get_connection</span><span class="p">(</span><span class="n">using</span><span class="p">)</span><span class="o">.</span><span class="n">savepoint</span><span class="p">()</span>

</div>
<div class="viewcode-block" id="savepoint_rollback"><a class="viewcode-back" href="../../../topics/db/transactions.html#django.db.transaction.savepoint_rollback">[docs]</a><span class="k">def</span> <span class="nf">savepoint_rollback</span><span class="p">(</span><span class="n">sid</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Rolls back the most recent savepoint (if one exists). Does nothing if</span>
<span class="sd">    savepoints are not supported.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">get_connection</span><span class="p">(</span><span class="n">using</span><span class="p">)</span><span class="o">.</span><span class="n">savepoint_rollback</span><span class="p">(</span><span class="n">sid</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="savepoint_commit"><a class="viewcode-back" href="../../../topics/db/transactions.html#django.db.transaction.savepoint_commit">[docs]</a><span class="k">def</span> <span class="nf">savepoint_commit</span><span class="p">(</span><span class="n">sid</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Commits the most recent savepoint (if one exists). Does nothing if</span>
<span class="sd">    savepoints are not supported.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">get_connection</span><span class="p">(</span><span class="n">using</span><span class="p">)</span><span class="o">.</span><span class="n">savepoint_commit</span><span class="p">(</span><span class="n">sid</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="clean_savepoints"><a class="viewcode-back" href="../../../topics/db/transactions.html#django.db.transaction.clean_savepoints">[docs]</a><span class="k">def</span> <span class="nf">clean_savepoints</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Resets the counter used to generate unique savepoint ids in this thread.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">get_connection</span><span class="p">(</span><span class="n">using</span><span class="p">)</span><span class="o">.</span><span class="n">clean_savepoints</span><span class="p">()</span>

</div>
<div class="viewcode-block" id="get_rollback"><a class="viewcode-back" href="../../../topics/db/transactions.html#django.db.transaction.get_rollback">[docs]</a><span class="k">def</span> <span class="nf">get_rollback</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Gets the &quot;needs rollback&quot; flag -- for *advanced use* only.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">get_connection</span><span class="p">(</span><span class="n">using</span><span class="p">)</span><span class="o">.</span><span class="n">get_rollback</span><span class="p">()</span>

</div>
<div class="viewcode-block" id="set_rollback"><a class="viewcode-back" href="../../../topics/db/transactions.html#django.db.transaction.set_rollback">[docs]</a><span class="k">def</span> <span class="nf">set_rollback</span><span class="p">(</span><span class="n">rollback</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Sets or unsets the &quot;needs rollback&quot; flag -- for *advanced use* only.</span>

<span class="sd">    When `rollback` is `True`, it triggers a rollback when exiting the</span>
<span class="sd">    innermost enclosing atomic block that has `savepoint=True` (that&#39;s the</span>
<span class="sd">    default). Use this to force a rollback without raising an exception.</span>

<span class="sd">    When `rollback` is `False`, it prevents such a rollback. Use this only</span>
<span class="sd">    after rolling back to a known-good state! Otherwise, you break the atomic</span>
<span class="sd">    block and data corruption may occur.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">get_connection</span><span class="p">(</span><span class="n">using</span><span class="p">)</span><span class="o">.</span><span class="n">set_rollback</span><span class="p">(</span><span class="n">rollback</span><span class="p">)</span>


<span class="c">#################################</span>
<span class="c"># Decorators / context managers #</span>
<span class="c">#################################</span>
</div>
<span class="k">class</span> <span class="nc">Atomic</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This class guarantees the atomic execution of a given block.</span>

<span class="sd">    An instance can be used either as a decorator or as a context manager.</span>

<span class="sd">    When it&#39;s used as a decorator, __call__ wraps the execution of the</span>
<span class="sd">    decorated function in the instance itself, used as a context manager.</span>

<span class="sd">    When it&#39;s used as a context manager, __enter__ creates a transaction or a</span>
<span class="sd">    savepoint, depending on whether a transaction is already in progress, and</span>
<span class="sd">    __exit__ commits the transaction or releases the savepoint on normal exit,</span>
<span class="sd">    and rolls back the transaction or to the savepoint on exceptions.</span>

<span class="sd">    It&#39;s possible to disable the creation of savepoints if the goal is to</span>
<span class="sd">    ensure that some code runs within a transaction without creating overhead.</span>

<span class="sd">    A stack of savepoints identifiers is maintained as an attribute of the</span>
<span class="sd">    connection. None denotes the absence of a savepoint.</span>

<span class="sd">    This allows reentrancy even if the same AtomicWrapper is reused. For</span>
<span class="sd">    example, it&#39;s possible to define `oa = @atomic(&#39;other&#39;)` and use `@oa` or</span>
<span class="sd">    `with oa:` multiple times.</span>

<span class="sd">    Since database connections are thread-local, this is thread-safe.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">using</span><span class="p">,</span> <span class="n">savepoint</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">using</span> <span class="o">=</span> <span class="n">using</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">savepoint</span> <span class="o">=</span> <span class="n">savepoint</span>

    <span class="k">def</span> <span class="nf">__enter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">connection</span> <span class="o">=</span> <span class="n">get_connection</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">using</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">connection</span><span class="o">.</span><span class="n">in_atomic_block</span><span class="p">:</span>
            <span class="c"># Reset state when entering an outermost atomic block.</span>
            <span class="n">connection</span><span class="o">.</span><span class="n">commit_on_exit</span> <span class="o">=</span> <span class="bp">True</span>
            <span class="n">connection</span><span class="o">.</span><span class="n">needs_rollback</span> <span class="o">=</span> <span class="bp">False</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">connection</span><span class="o">.</span><span class="n">get_autocommit</span><span class="p">():</span>
                <span class="c"># Some database adapters (namely sqlite3) don&#39;t handle</span>
                <span class="c"># transactions and savepoints properly when autocommit is off.</span>
                <span class="c"># Turning autocommit back on isn&#39;t an option; it would trigger</span>
                <span class="c"># a premature commit. Give up if that happens.</span>
                <span class="k">if</span> <span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">autocommits_when_autocommit_is_off</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="n">TransactionManagementError</span><span class="p">(</span>
                        <span class="s">&quot;Your database backend doesn&#39;t behave properly when &quot;</span>
                        <span class="s">&quot;autocommit is off. Turn it on before using &#39;atomic&#39;.&quot;</span><span class="p">)</span>
                <span class="c"># When entering an atomic block with autocommit turned off,</span>
                <span class="c"># Django should only use savepoints and shouldn&#39;t commit.</span>
                <span class="c"># This requires at least a savepoint for the outermost block.</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">savepoint</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="n">TransactionManagementError</span><span class="p">(</span>
                        <span class="s">&quot;The outermost &#39;atomic&#39; block cannot use &quot;</span>
                        <span class="s">&quot;savepoint = False when autocommit is off.&quot;</span><span class="p">)</span>
                <span class="c"># Pretend we&#39;re already in an atomic block to bypass the code</span>
                <span class="c"># that disables autocommit to enter a transaction, and make a</span>
                <span class="c"># note to deal with this case in __exit__.</span>
                <span class="n">connection</span><span class="o">.</span><span class="n">in_atomic_block</span> <span class="o">=</span> <span class="bp">True</span>
                <span class="n">connection</span><span class="o">.</span><span class="n">commit_on_exit</span> <span class="o">=</span> <span class="bp">False</span>

        <span class="k">if</span> <span class="n">connection</span><span class="o">.</span><span class="n">in_atomic_block</span><span class="p">:</span>
            <span class="c"># We&#39;re already in a transaction; create a savepoint, unless we</span>
            <span class="c"># were told not to or we&#39;re already waiting for a rollback. The</span>
            <span class="c"># second condition avoids creating useless savepoints and prevents</span>
            <span class="c"># overwriting needs_rollback until the rollback is performed.</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">savepoint</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">connection</span><span class="o">.</span><span class="n">needs_rollback</span><span class="p">:</span>
                <span class="n">sid</span> <span class="o">=</span> <span class="n">connection</span><span class="o">.</span><span class="n">savepoint</span><span class="p">()</span>
                <span class="n">connection</span><span class="o">.</span><span class="n">savepoint_ids</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">sid</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">connection</span><span class="o">.</span><span class="n">savepoint_ids</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="bp">None</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c"># We aren&#39;t in a transaction yet; create one.</span>
            <span class="c"># The usual way to start a transaction is to turn autocommit off.</span>
            <span class="c"># However, some database adapters (namely sqlite3) don&#39;t handle</span>
            <span class="c"># transactions and savepoints properly when autocommit is off.</span>
            <span class="c"># In such cases, start an explicit transaction instead, which has</span>
            <span class="c"># the side-effect of disabling autocommit.</span>
            <span class="k">if</span> <span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">autocommits_when_autocommit_is_off</span><span class="p">:</span>
                <span class="n">connection</span><span class="o">.</span><span class="n">_start_transaction_under_autocommit</span><span class="p">()</span>
                <span class="n">connection</span><span class="o">.</span><span class="n">autocommit</span> <span class="o">=</span> <span class="bp">False</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">connection</span><span class="o">.</span><span class="n">set_autocommit</span><span class="p">(</span><span class="bp">False</span><span class="p">)</span>
            <span class="n">connection</span><span class="o">.</span><span class="n">in_atomic_block</span> <span class="o">=</span> <span class="bp">True</span>

    <span class="k">def</span> <span class="nf">__exit__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exc_type</span><span class="p">,</span> <span class="n">exc_value</span><span class="p">,</span> <span class="n">traceback</span><span class="p">):</span>
        <span class="n">connection</span> <span class="o">=</span> <span class="n">get_connection</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">using</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">connection</span><span class="o">.</span><span class="n">savepoint_ids</span><span class="p">:</span>
            <span class="n">sid</span> <span class="o">=</span> <span class="n">connection</span><span class="o">.</span><span class="n">savepoint_ids</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c"># Prematurely unset this flag to allow using commit or rollback.</span>
            <span class="n">connection</span><span class="o">.</span><span class="n">in_atomic_block</span> <span class="o">=</span> <span class="bp">False</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">connection</span><span class="o">.</span><span class="n">closed_in_transaction</span><span class="p">:</span>
                <span class="c"># The database will perform a rollback by itself.</span>
                <span class="c"># Wait until we exit the outermost block.</span>
                <span class="k">pass</span>

            <span class="k">elif</span> <span class="n">exc_type</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">connection</span><span class="o">.</span><span class="n">needs_rollback</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">connection</span><span class="o">.</span><span class="n">in_atomic_block</span><span class="p">:</span>
                    <span class="c"># Release savepoint if there is one</span>
                    <span class="k">if</span> <span class="n">sid</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
                        <span class="k">try</span><span class="p">:</span>
                            <span class="n">connection</span><span class="o">.</span><span class="n">savepoint_commit</span><span class="p">(</span><span class="n">sid</span><span class="p">)</span>
                        <span class="k">except</span> <span class="n">DatabaseError</span><span class="p">:</span>
                            <span class="k">try</span><span class="p">:</span>
                                <span class="n">connection</span><span class="o">.</span><span class="n">savepoint_rollback</span><span class="p">(</span><span class="n">sid</span><span class="p">)</span>
                            <span class="k">except</span> <span class="n">Error</span><span class="p">:</span>
                                <span class="c"># If rolling back to a savepoint fails, mark for</span>
                                <span class="c"># rollback at a higher level and avoid shadowing</span>
                                <span class="c"># the original exception.</span>
                                <span class="n">connection</span><span class="o">.</span><span class="n">needs_rollback</span> <span class="o">=</span> <span class="bp">True</span>
                            <span class="k">raise</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="c"># Commit transaction</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="n">connection</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span>
                    <span class="k">except</span> <span class="n">DatabaseError</span><span class="p">:</span>
                        <span class="k">try</span><span class="p">:</span>
                            <span class="n">connection</span><span class="o">.</span><span class="n">rollback</span><span class="p">()</span>
                        <span class="k">except</span> <span class="n">Error</span><span class="p">:</span>
                            <span class="c"># An error during rollback means that something</span>
                            <span class="c"># went wrong with the connection. Drop it.</span>
                            <span class="n">connection</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
                        <span class="k">raise</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c"># This flag will be set to True again if there isn&#39;t a savepoint</span>
                <span class="c"># allowing to perform the rollback at this level.</span>
                <span class="n">connection</span><span class="o">.</span><span class="n">needs_rollback</span> <span class="o">=</span> <span class="bp">False</span>
                <span class="k">if</span> <span class="n">connection</span><span class="o">.</span><span class="n">in_atomic_block</span><span class="p">:</span>
                    <span class="c"># Roll back to savepoint if there is one, mark for rollback</span>
                    <span class="c"># otherwise.</span>
                    <span class="k">if</span> <span class="n">sid</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
                        <span class="n">connection</span><span class="o">.</span><span class="n">needs_rollback</span> <span class="o">=</span> <span class="bp">True</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="k">try</span><span class="p">:</span>
                            <span class="n">connection</span><span class="o">.</span><span class="n">savepoint_rollback</span><span class="p">(</span><span class="n">sid</span><span class="p">)</span>
                        <span class="k">except</span> <span class="n">Error</span><span class="p">:</span>
                            <span class="c"># If rolling back to a savepoint fails, mark for</span>
                            <span class="c"># rollback at a higher level and avoid shadowing</span>
                            <span class="c"># the original exception.</span>
                            <span class="n">connection</span><span class="o">.</span><span class="n">needs_rollback</span> <span class="o">=</span> <span class="bp">True</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="c"># Roll back transaction</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="n">connection</span><span class="o">.</span><span class="n">rollback</span><span class="p">()</span>
                    <span class="k">except</span> <span class="n">Error</span><span class="p">:</span>
                        <span class="c"># An error during rollback means that something</span>
                        <span class="c"># went wrong with the connection. Drop it.</span>
                        <span class="n">connection</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

        <span class="k">finally</span><span class="p">:</span>
            <span class="c"># Outermost block exit when autocommit was enabled.</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">connection</span><span class="o">.</span><span class="n">in_atomic_block</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">connection</span><span class="o">.</span><span class="n">closed_in_transaction</span><span class="p">:</span>
                    <span class="n">connection</span><span class="o">.</span><span class="n">connection</span> <span class="o">=</span> <span class="bp">None</span>
                <span class="k">elif</span> <span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">autocommits_when_autocommit_is_off</span><span class="p">:</span>
                    <span class="n">connection</span><span class="o">.</span><span class="n">autocommit</span> <span class="o">=</span> <span class="bp">True</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">connection</span><span class="o">.</span><span class="n">set_autocommit</span><span class="p">(</span><span class="bp">True</span><span class="p">)</span>
            <span class="c"># Outermost block exit when autocommit was disabled.</span>
            <span class="k">elif</span> <span class="ow">not</span> <span class="n">connection</span><span class="o">.</span><span class="n">savepoint_ids</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">connection</span><span class="o">.</span><span class="n">commit_on_exit</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">connection</span><span class="o">.</span><span class="n">closed_in_transaction</span><span class="p">:</span>
                    <span class="n">connection</span><span class="o">.</span><span class="n">connection</span> <span class="o">=</span> <span class="bp">None</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">connection</span><span class="o">.</span><span class="n">in_atomic_block</span> <span class="o">=</span> <span class="bp">False</span>

    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">func</span><span class="p">):</span>
        <span class="nd">@wraps</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">assigned</span><span class="o">=</span><span class="n">available_attrs</span><span class="p">(</span><span class="n">func</span><span class="p">))</span>
        <span class="k">def</span> <span class="nf">inner</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="k">with</span> <span class="bp">self</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">inner</span>


<div class="viewcode-block" id="atomic"><a class="viewcode-back" href="../../../topics/db/transactions.html#django.db.transaction.atomic">[docs]</a><span class="k">def</span> <span class="nf">atomic</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">savepoint</span><span class="o">=</span><span class="bp">True</span><span class="p">):</span>
    <span class="c"># Bare decorator: @atomic -- although the first argument is called</span>
    <span class="c"># `using`, it&#39;s actually the function being decorated.</span>
    <span class="k">if</span> <span class="nb">callable</span><span class="p">(</span><span class="n">using</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">Atomic</span><span class="p">(</span><span class="n">DEFAULT_DB_ALIAS</span><span class="p">,</span> <span class="n">savepoint</span><span class="p">)(</span><span class="n">using</span><span class="p">)</span>
    <span class="c"># Decorator: @atomic(...) or context manager: with atomic(...): ...</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">Atomic</span><span class="p">(</span><span class="n">using</span><span class="p">,</span> <span class="n">savepoint</span><span class="p">)</span>

</div>
<span class="k">def</span> <span class="nf">_non_atomic_requests</span><span class="p">(</span><span class="n">view</span><span class="p">,</span> <span class="n">using</span><span class="p">):</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">view</span><span class="o">.</span><span class="n">_non_atomic_requests</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">using</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
        <span class="n">view</span><span class="o">.</span><span class="n">_non_atomic_requests</span> <span class="o">=</span> <span class="nb">set</span><span class="p">([</span><span class="n">using</span><span class="p">])</span>
    <span class="k">return</span> <span class="n">view</span>


<div class="viewcode-block" id="non_atomic_requests"><a class="viewcode-back" href="../../../topics/db/transactions.html#django.db.transaction.non_atomic_requests">[docs]</a><span class="k">def</span> <span class="nf">non_atomic_requests</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="k">if</span> <span class="nb">callable</span><span class="p">(</span><span class="n">using</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">_non_atomic_requests</span><span class="p">(</span><span class="n">using</span><span class="p">,</span> <span class="n">DEFAULT_DB_ALIAS</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">using</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="n">using</span> <span class="o">=</span> <span class="n">DEFAULT_DB_ALIAS</span>
        <span class="k">return</span> <span class="k">lambda</span> <span class="n">view</span><span class="p">:</span> <span class="n">_non_atomic_requests</span><span class="p">(</span><span class="n">view</span><span class="p">,</span> <span class="n">using</span><span class="p">)</span>


<span class="c">############################################</span>
<span class="c"># Deprecated decorators / context managers #</span>
<span class="c">############################################</span>
</div>
<span class="k">class</span> <span class="nc">Transaction</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Acts as either a decorator, or a context manager.  If it&#39;s a decorator it</span>
<span class="sd">    takes a function and returns a wrapped function.  If it&#39;s a contextmanager</span>
<span class="sd">    it&#39;s used with the ``with`` statement.  In either event entering/exiting</span>
<span class="sd">    are called before and after, respectively, the function/block is executed.</span>

<span class="sd">    autocommit, commit_on_success, and commit_manually contain the</span>
<span class="sd">    implementations of entering and exiting.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">entering</span><span class="p">,</span> <span class="n">exiting</span><span class="p">,</span> <span class="n">using</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">entering</span> <span class="o">=</span> <span class="n">entering</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">exiting</span> <span class="o">=</span> <span class="n">exiting</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">using</span> <span class="o">=</span> <span class="n">using</span>

    <span class="k">def</span> <span class="nf">__enter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">entering</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">using</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__exit__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exc_type</span><span class="p">,</span> <span class="n">exc_value</span><span class="p">,</span> <span class="n">traceback</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">exiting</span><span class="p">(</span><span class="n">exc_type</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">using</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">func</span><span class="p">):</span>
        <span class="nd">@wraps</span><span class="p">(</span><span class="n">func</span><span class="p">)</span>
        <span class="k">def</span> <span class="nf">inner</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
            <span class="k">with</span> <span class="bp">self</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">inner</span>


<span class="k">def</span> <span class="nf">_transaction_func</span><span class="p">(</span><span class="n">entering</span><span class="p">,</span> <span class="n">exiting</span><span class="p">,</span> <span class="n">using</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Takes 3 things, an entering function (what to do to start this block of</span>
<span class="sd">    transaction management), an exiting function (what to do to end it, on both</span>
<span class="sd">    success and failure, and using which can be: None, indicating using is</span>
<span class="sd">    DEFAULT_DB_ALIAS, a callable, indicating that using is DEFAULT_DB_ALIAS and</span>
<span class="sd">    to return the function already wrapped.</span>

<span class="sd">    Returns either a Transaction objects, which is both a decorator and a</span>
<span class="sd">    context manager, or a wrapped function, if using is a callable.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c"># Note that although the first argument is *called* `using`, it</span>
    <span class="c"># may actually be a function; @autocommit and @autocommit(&#39;foo&#39;)</span>
    <span class="c"># are both allowed forms.</span>
    <span class="k">if</span> <span class="n">using</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
        <span class="n">using</span> <span class="o">=</span> <span class="n">DEFAULT_DB_ALIAS</span>
    <span class="k">if</span> <span class="nb">callable</span><span class="p">(</span><span class="n">using</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">Transaction</span><span class="p">(</span><span class="n">entering</span><span class="p">,</span> <span class="n">exiting</span><span class="p">,</span> <span class="n">DEFAULT_DB_ALIAS</span><span class="p">)(</span><span class="n">using</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">Transaction</span><span class="p">(</span><span class="n">entering</span><span class="p">,</span> <span class="n">exiting</span><span class="p">,</span> <span class="n">using</span><span class="p">)</span>


<div class="viewcode-block" id="autocommit"><a class="viewcode-back" href="../../../topics/db/transactions.html#django.db.transaction.autocommit">[docs]</a><span class="k">def</span> <span class="nf">autocommit</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Decorator that activates commit on save. This is Django&#39;s default behavior;</span>
<span class="sd">    this decorator is useful if you globally activated transaction management in</span>
<span class="sd">    your settings file and want the default behavior in some view functions.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s">&quot;autocommit is deprecated in favor of set_autocommit.&quot;</span><span class="p">,</span>
        <span class="n">RemovedInDjango18Warning</span><span class="p">,</span> <span class="n">stacklevel</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">entering</span><span class="p">(</span><span class="n">using</span><span class="p">):</span>
        <span class="n">enter_transaction_management</span><span class="p">(</span><span class="n">managed</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="n">using</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">exiting</span><span class="p">(</span><span class="n">exc_type</span><span class="p">,</span> <span class="n">using</span><span class="p">):</span>
        <span class="n">leave_transaction_management</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="n">using</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">_transaction_func</span><span class="p">(</span><span class="n">entering</span><span class="p">,</span> <span class="n">exiting</span><span class="p">,</span> <span class="n">using</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="commit_on_success"><a class="viewcode-back" href="../../../topics/db/transactions.html#django.db.transaction.commit_on_success">[docs]</a><span class="k">def</span> <span class="nf">commit_on_success</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    This decorator activates commit on response. This way, if the view function</span>
<span class="sd">    runs successfully, a commit is made; if the viewfunc produces an exception,</span>
<span class="sd">    a rollback is made. This is one of the most common ways to do transaction</span>
<span class="sd">    control in Web apps.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s">&quot;commit_on_success is deprecated in favor of atomic.&quot;</span><span class="p">,</span>
        <span class="n">RemovedInDjango18Warning</span><span class="p">,</span> <span class="n">stacklevel</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">entering</span><span class="p">(</span><span class="n">using</span><span class="p">):</span>
        <span class="n">enter_transaction_management</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="n">using</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">exiting</span><span class="p">(</span><span class="n">exc_type</span><span class="p">,</span> <span class="n">using</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">exc_type</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">is_dirty</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="n">using</span><span class="p">):</span>
                    <span class="n">rollback</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="n">using</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">is_dirty</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="n">using</span><span class="p">):</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="n">commit</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="n">using</span><span class="p">)</span>
                    <span class="k">except</span><span class="p">:</span>
                        <span class="n">rollback</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="n">using</span><span class="p">)</span>
                        <span class="k">raise</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="n">leave_transaction_management</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="n">using</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">_transaction_func</span><span class="p">(</span><span class="n">entering</span><span class="p">,</span> <span class="n">exiting</span><span class="p">,</span> <span class="n">using</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="commit_manually"><a class="viewcode-back" href="../../../topics/db/transactions.html#django.db.transaction.commit_manually">[docs]</a><span class="k">def</span> <span class="nf">commit_manually</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Decorator that activates manual transaction control. It just disables</span>
<span class="sd">    automatic transaction control and doesn&#39;t do any commit/rollback of its</span>
<span class="sd">    own -- it&#39;s up to the user to call the commit and rollback functions</span>
<span class="sd">    themselves.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s">&quot;commit_manually is deprecated in favor of set_autocommit.&quot;</span><span class="p">,</span>
        <span class="n">RemovedInDjango18Warning</span><span class="p">,</span> <span class="n">stacklevel</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">entering</span><span class="p">(</span><span class="n">using</span><span class="p">):</span>
        <span class="n">enter_transaction_management</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="n">using</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">exiting</span><span class="p">(</span><span class="n">exc_type</span><span class="p">,</span> <span class="n">using</span><span class="p">):</span>
        <span class="n">leave_transaction_management</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="n">using</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">_transaction_func</span><span class="p">(</span><span class="n">entering</span><span class="p">,</span> <span class="n">exiting</span><span class="p">,</span> <span class="n">using</span><span class="p">)</span>

</div>
<span class="k">def</span> <span class="nf">commit_on_success_unless_managed</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">savepoint</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Transitory API to preserve backwards-compatibility while refactoring.</span>

<span class="sd">    Once the legacy transaction management is fully deprecated, this should</span>
<span class="sd">    simply be replaced by atomic. Until then, it&#39;s necessary to guarantee that</span>
<span class="sd">    a commit occurs on exit, which atomic doesn&#39;t do when it&#39;s nested.</span>

<span class="sd">    Unlike atomic, savepoint defaults to False because that&#39;s closer to the</span>
<span class="sd">    legacy behavior.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">connection</span> <span class="o">=</span> <span class="n">get_connection</span><span class="p">(</span><span class="n">using</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">connection</span><span class="o">.</span><span class="n">get_autocommit</span><span class="p">()</span> <span class="ow">or</span> <span class="n">connection</span><span class="o">.</span><span class="n">in_atomic_block</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">atomic</span><span class="p">(</span><span class="n">using</span><span class="p">,</span> <span class="n">savepoint</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">def</span> <span class="nf">entering</span><span class="p">(</span><span class="n">using</span><span class="p">):</span>
            <span class="k">pass</span>

        <span class="k">def</span> <span class="nf">exiting</span><span class="p">(</span><span class="n">exc_type</span><span class="p">,</span> <span class="n">using</span><span class="p">):</span>
            <span class="n">set_dirty</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="n">using</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">_transaction_func</span><span class="p">(</span><span class="n">entering</span><span class="p">,</span> <span class="n">exiting</span><span class="p">,</span> <span class="n">using</span><span class="p">)</span>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3>Browse</h3>
  <ul>
    
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../../index.html">Module code</a>
        
          <ul><li><a href="../../django.html">django</a>
        
          <ul><li><a href="../db.html">django.db</a>
        
        <ul><li>django.db.transaction</li></ul>
        </li></ul></li></ul></li></ul>
      </li>
  </ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>